package com.evolve.webbot.robot;
import  java.util.Properties;
import  java.util.Date;
import  java.awt.Robot;
import  javax.swing.JOptionPane;
import  java.awt.event.InputEvent;
import  java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import  javax.swing.JTextArea;
import  java.text.SimpleDateFormat;
import  org.apache.log4j.Logger;

public class RobotRL1
{   
    static Logger log = Logger.getLogger( RobotRL1.class ); 
    private String path;
    public static String WB_MAIN_DIR     = "evolve-webbot";
    public static String RETAIL_MAIN_DIR = "retail-webbot";
    public static String RETAIL_PROP_DIR = "retail-properties";
    public static String RETAIL_PROP_ARC = "retailPortal.properties";
    public static String RETAIL_FILE_DIR = "retail-files";
    
    public RobotRL1(){}
    
    public RobotRL1(String path)
    {
        this.path = path;
    }
    
    public boolean capturaReporte()
    {
        boolean    esReporte = false;
        Properties p = new Properties();
        
        Robot  robot        = null; 
        Date   fechaEjecuta = new Date();
        String fechaRobot   = null;
        String usr = "";
        String pwd = "";
        
//        try{ 
//             log.info("[-]lanzando ie");
//             Runtime runinstance = Runtime.getRuntime();
//             runinstance.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
//           } catch(Exception e){log.error("[-]error al iniciar ie ",e);}        
        
        try{        
             robot = new Robot();      
           } catch(Exception e){log.info("[-]error al iniciar robot ",e);} 
        
        
      try{
           
           usr = p.getProperty("Portal.user");
           pwd = p.getProperty("Portal.passwd");
           
           FileInputStream fis = new FileInputStream(this.path+File.separator+WB_MAIN_DIR+
                                                               File.separator+RETAIL_MAIN_DIR+
                                                               File.separator+RETAIL_PROP_DIR+
                                                               File.separator+RETAIL_PROP_ARC);
           p.load(fis);
           
          }catch(IOException ioe){log.error("[-]error al recuperar archivo de properties robot retail link",ioe);
      }
        
        
        try{
            Thread.sleep(25000);
           }catch(Exception e){};
            
            /*inicio login*/
            robot.mouseMove(1200, 450);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                 Thread.sleep(10000);
               }catch(Exception e){};
               
//            this.tecleaCadena(robot,"MEG4777");
//            
//            try{
//                 Thread.sleep(2500);
//               }catch(Exception e){};
//            
//            this.tecleaCadena(robot,"LAmisma01");
            
            robot.keyPress(KeyEvent.VK_CAPS_LOCK);
                        
            robot.keyPress(KeyEvent.VK_M);
            robot.keyRelease(KeyEvent.VK_M);
            robot.keyPress(KeyEvent.VK_E);
            robot.keyRelease(KeyEvent.VK_E);
            robot.keyPress(KeyEvent.VK_G);
            robot.keyRelease(KeyEvent.VK_G);            
            robot.keyPress(KeyEvent.VK_4);
            robot.keyRelease(KeyEvent.VK_4);            
            robot.keyPress(KeyEvent.VK_7);
            robot.keyRelease(KeyEvent.VK_7);
            robot.keyPress(KeyEvent.VK_7);
            robot.keyRelease(KeyEvent.VK_7);            
            robot.keyPress(KeyEvent.VK_7);
            robot.keyRelease(KeyEvent.VK_7); 
            
            try{
                 Thread.sleep(2500);
               }catch(Exception e){};            
            
            robot.keyRelease(KeyEvent.VK_CAPS_LOCK);           
            
            robot.mouseMove(1200,480);
            
            robot.mousePress(InputEvent.BUTTON1_MASK);            
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            
            
            robot.keyPress(KeyEvent.VK_CAPS_LOCK);
            robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
             
            robot.keyPress(KeyEvent.VK_CAPS_LOCK);
            robot.keyPress(KeyEvent.VK_L);
            robot.keyRelease(KeyEvent.VK_L);            
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);            
            robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  

            try{
                 Thread.sleep(6000);
               }catch(Exception e){};
            
            robot.keyPress(KeyEvent.VK_CAPS_LOCK);
            robot.keyPress(KeyEvent.VK_M);
            robot.keyRelease(KeyEvent.VK_M);
            robot.keyPress(KeyEvent.VK_I);
            robot.keyRelease(KeyEvent.VK_I);
            robot.keyPress(KeyEvent.VK_S);
            robot.keyRelease(KeyEvent.VK_S);
            robot.keyPress(KeyEvent.VK_M);
            robot.keyRelease(KeyEvent.VK_M);
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);
            robot.keyPress(KeyEvent.VK_0);
            robot.keyRelease(KeyEvent.VK_0);
            robot.keyPress(KeyEvent.VK_1);
            robot.keyRelease(KeyEvent.VK_1);
            robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
            robot.mouseMove(1200,625);
            
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            System.out.println("fin login");
            
            /*fin login*/
           try{
                Thread.sleep(120000);
              }catch(Exception e){};

/*-------------------------*/
/*-------------------------*/        
            /*inicia columns*/
            System.out.println("inicia columns");
            System.out.println("abre store information");
            //robot.mouseMove(970,220);
            robot.mouseMove(975,220);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
          try{
                Thread.sleep(6000);
              }catch(Exception e){};
            robot.mouseMove(995,265);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
          try{
                Thread.sleep(6000);
              }catch(Exception e){};            
            robot.mouseMove(995,245);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
          try{
                Thread.sleep(6000);
              }catch(Exception e){};            
            robot.mouseMove(995,230);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            
                      try{
                Thread.sleep(6000);
              }catch(Exception e){};            
            robot.mouseMove(975,220);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        try{
                Thread.sleep(6000);
              }catch(Exception e){};            

            System.out.println("fin store information");

            
            System.out.println("item information");
            
            robot.mouseMove(970,200);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
           try{
                Thread.sleep(6000);
              }catch(Exception e){};            
            
            robot.mouseMove(990,215);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                Thread.sleep(6000);
              }catch(Exception e){};              
            
            robot.mouseMove(990,440);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                Thread.sleep(6000);
              }catch(Exception e){};
              
            robot.mouseMove(990,210);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            try{
                Thread.sleep(6000);
              }catch(Exception e){};            
            robot.mouseMove(970,200);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                 Thread.sleep(6000);
               }catch(Exception e){};            
            
            robot.mouseMove(970,250);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
            try{
                 Thread.sleep(6000);
               }catch(Exception e){};            
            
            robot.mouseMove(990,280);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};            
            
            robot.mouseMove(990,310);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};            
            
            robot.mouseMove(970,250);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
            /*presiona POS*/
            try{
                 Thread.sleep(10000);
               }catch(Exception e){};
            
            robot.mouseMove(970,270);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};
              
            /*abre POS QTY*/  
            robot.mouseMove(990,350);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            
            
            try{
                 Thread.sleep(6000);
               }catch(Exception e){};            
            
            /*abre POS SALES*/            
            robot.mouseMove(992,330);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};               
            
            robot.mouseMove(970,270);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);             
                        
            System.out.println("termina columns");
            /*cierra POS*/            
            
            try{
                Thread.sleep(5000);
              }catch(Exception e){};            
            /*inicia items*/
            System.out.println("inicia items");           
            robot.mouseMove(1000,110);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
            try{
                Thread.sleep(6000);
             }catch(Exception e){};            
            
            robot.mouseMove(840,200);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

           try{
               Thread.sleep(6000);
              }catch(Exception e){};            
            
            robot.mouseMove(1100,230);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            
            
            try{
                Thread.sleep(6000);
              }catch(Exception e){};            
            
            robot.mouseMove(1050,310);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);    

            try{
                Thread.sleep(6000);
              }catch(Exception e){}; 
              
             /*inicia locations*/
            robot.mouseMove(1110,110);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            

            try{
                Thread.sleep(5000);
              }catch(Exception e){};            
            
            robot.mouseMove(840,200);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);  

            try{
                Thread.sleep(5000);
              }catch(Exception e){};

            robot.mouseMove(980,210);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

            try{
                Thread.sleep(5000);
              }catch(Exception e){};            
            robot.mouseMove(1000,250);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);              
            /*termina locations*/ 
            
            System.out.println("Termina Locations");    
            try{
                Thread.sleep(5000);
              }catch(Exception e){};             
            System.out.println("Inicia Times");
            
           /*inicia range 1*/
            robot.mouseMove(1260,110);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);                
            
            try{
                Thread.sleep(6000);
              }catch(Exception e){}; 

            /*inicia range 1*/
            try{
                Thread.sleep(6000);
               }catch(Exception e){};
               
            robot.mouseMove(860,200);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){}; 
               
            robot.mouseMove(970,290);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);               
      
            try{
                Thread.sleep(6000);
               }catch(Exception e){};      

            robot.mouseMove(1010,310);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);                              
       
                       try{
                Thread.sleep(6000);
               }catch(Exception e){};      

            robot.mouseMove(1250,310);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);             
            
            log.info("[-]operacion en pantalla de times ");
            
            SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
            SimpleDateFormat sdf2 = new SimpleDateFormat("dd");
            SimpleDateFormat sdf3 = new SimpleDateFormat("YYYY");
            
            log.info("[-]se ingresa la fecha de programacion del reporte ");
            
            fechaRobot =sdf1.format(fechaEjecuta);
            tecleaFecha(fechaRobot,robot);  
            tecleaEspacio(robot);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};  
               
            fechaRobot =sdf2.format(fechaEjecuta);
            tecleaFecha(fechaRobot,robot);  
            tecleaEspacio(robot);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};            
            
            fechaRobot =sdf3.format(fechaEjecuta);
            tecleaFecha(fechaRobot,robot);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};  
               
            robot.mouseMove(1250,360);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);                 
            
            log.info("[-]confirmacion de fecha ");

            fechaRobot =sdf1.format(fechaEjecuta);
            tecleaFecha(fechaRobot,robot);  
            tecleaEspacio(robot);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};            
            
            fechaRobot =sdf2.format(fechaEjecuta);
            tecleaFecha(fechaRobot,robot);  
            tecleaEspacio(robot);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};
               
            fechaRobot =sdf3.format(fechaEjecuta);
            tecleaFecha(fechaRobot,robot);            
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};            
            
            robot.mouseMove(1270,395);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);            
            
            log.info("[-]fin de ingreso de fecha programacion de reporte ");
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};
               
            log.info("[-]operacion en pantalla de submit");
            
            robot.mouseMove(1550,110);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};
               
            robot.mouseMove(975,315);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);                 
            
            try{
                Thread.sleep(6000);
               }catch(Exception e){};            
            
            robot.mouseMove(1190,210);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
            log.info("[-]fin de operacion pantalla de submit");
            
            log.info("[-]cierre de ie");
            
            try{
                Thread.sleep(5000);
               }catch(Exception e){};
               
            robot.mouseMove(1570,10);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
           
            try{
                Thread.sleep(5000);
               }catch(Exception e){};
            
            robot.mouseMove(0,0);
            
            return esReporte=true;
    }
   
    public void tecleaEspacio(Robot robot)
    {
       robot.keyPress(45);
       robot.keyRelease(45); 
    }
    
    public void tecleaFecha(String fechaBusqueda,Robot robot)
    {
        int k=0;
        
        for(int i=0;i<fechaBusqueda.length();i++)
        {
            k=Integer.parseInt(String.valueOf(fechaBusqueda.charAt(i)));
            
            switch(k)
            {
                case 0:   robot.keyPress(KeyEvent.VK_0);
                          robot.keyRelease(KeyEvent.VK_0);
                          break;
                case 1:   robot.keyPress(KeyEvent.VK_1);
                          robot.keyRelease(KeyEvent.VK_1);
                          break;
                case 2:   robot.keyPress(KeyEvent.VK_2);
                          robot.keyRelease(KeyEvent.VK_2);
                          break;    
                case 3:   robot.keyPress(KeyEvent.VK_3);
                          robot.keyRelease(KeyEvent.VK_3);
                          break;  
                case 4:   robot.keyPress(KeyEvent.VK_4);
                          robot.keyRelease(KeyEvent.VK_4);
                          break; 
                case 5:   robot.keyPress(KeyEvent.VK_5);
                          robot.keyRelease(KeyEvent.VK_5);
                          break;   
                case 6:   robot.keyPress(KeyEvent.VK_6);
                          robot.keyRelease(KeyEvent.VK_6);
                          break;  
                case 7:   robot.keyPress(KeyEvent.VK_7);
                          robot.keyRelease(KeyEvent.VK_7);
                          break;  
                case 8:   robot.keyPress(KeyEvent.VK_8);
                          robot.keyRelease(KeyEvent.VK_8);
                          break;                    
                case 9:   robot.keyPress(KeyEvent.VK_9);
                          robot.keyRelease(KeyEvent.VK_9);
                          break;
                case 10: 
                          robot.keyPress(45);
                          robot.keyRelease(45);
                    break;
                default: System.out.println("Action"); 
                          break;                    
            }
        }        
    }
    
    public void tecleaCadena(Robot robot,String cadena)
    {
        int  keyValue = 0;
        char array[]  = new char[cadena.length()];
        
        array = cadena.toCharArray();
       
        robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
        
        if(0 < array.length)
        {
          for(int i=0;i<array.length;i++)
          {
            keyValue = Integer.valueOf(array[i]); 
            
            if(keyValue >= 65 &&
               keyValue <= 90)
            {
              System.out.println(""+keyValue+" - "+array[i]);
              robot.keyPress(KeyEvent.VK_CAPS_LOCK);
              System.out.println("e"+KeyEvent.VK_E);
              try{
                   Thread.sleep(2500);
                 }catch(Exception e){};  
                 
              robot.keyPress(keyValue);
              try{
                   Thread.sleep(2000);
                 }catch(Exception e){};               
              robot.keyRelease(keyValue);
              
              /*robot.keyRelease(KeyEvent.VK_CAPS_LOCK);*/
              
              if(i==cadena.length())
              {
                  robot.keyPress(KeyEvent.VK_CAPS_LOCK);
                  robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
              }
            }
            else
            {
              robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
              
              try{
                   Thread.sleep(2000);
                 }catch(Exception e){};  
                 
              robot.keyPress(keyValue);
              
              try{
                   Thread.sleep(2000);
                 }catch(Exception e){};                
                 
              robot.keyRelease(keyValue);
              
            }    
            
            try{
                   Thread.sleep(2000);
               }catch(Exception e){}; 
          }        
        }
    }
    
    public static void main( String args[] )   
    {
        RobotRL1 robot = new RobotRL1();
        robot.capturaReporte();
    } 
}
